﻿using Ndns.ServerHost.Abstractions;
using System;
using System.Threading.Tasks;

namespace Ndns.ServerHost
{
    class MiddlewareNode : IDisposable
    {
        public IMiddleware Middleware { get; set; }
        public RequestDelegate Handler { get; set; }
        public MiddlewareNode Next { get; set; }

        public MiddlewareNode(IMiddleware middleware)
        {
            Middleware = middleware;
        }
        public MiddlewareNode(RequestDelegate handler)
        {
            Handler = handler;
        }

        public Task InvokeAsync(DnsContext context)
        {
            if (Middleware != null)
                return Middleware.InvokeAsync(context);
            else
                return Handler(context);
        }


        public void Dispose()
        {
            Middleware?.Dispose();
            Next?.Dispose();
        }
    }
}
